<?php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Migrations\Migration;

use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        app(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();

        //创建权限
        Permission::create(['name'=>'manage_contents']);
        Permission::create(['name'=>'manage_users']);
        Permission::create(['name'=>'edit_settings']);


        //创建角色
        $founder = Role::create(["name" => 'Founder']);
        $founder->givePermissionTo('manage_contents');
        $founder->givePermissionTo('manage_users');
        $founder->givePermissionTo('edit_settings');


        $maintainer = Role::create(["name" => 'Maintainer']);
        $maintainer->givePermissionTo('manage_contents');
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
        app(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
        Model::unguard();
        $table_names = config('permission.table_names');
        DB::table($table_names['role_has_permissions'])->delete();
        DB::table($table_names['model_has_roles'])->delete();
        DB::table($table_names['model_has_permissions'])->delete();
        DB::table($table_names['roles'])->delete();
        DB::table($table_names['permissions'])->delete();
        Model::reguard();
    }
};
